package com.hzdl.car.controller;

import com.github.pagehelper.PageInfo;
import com.hzdl.car.bean.Menu;
import com.hzdl.car.service.MenuService;
import com.hzdl.car.util.AJAXResult;
import com.hzdl.car.util.MenuTree;
import com.hzdl.car.vo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/menu")
public class MenuController {


    @Autowired
    private MenuService ms;
    @RequestMapping("/menuManagerInit")
    public String menuManagerInit(){
        return "system/menu/menu";
    }


    @RequestMapping("/menuLeftInit")
    public String menuLeftInit(){
        return "system/menu/menuLeft";
    }

    @RequestMapping("/menuRightInit")
    public String menuRightInit(){
        return "system/menu/menuRight";
    }

/*
Ajax请求
 */
@RequestMapping("/menuLeftDtreeShow")
@ResponseBody
    public AJAXResult menuLeftDtreeShow(){
    List<Menu> menuList = ms.findAll();
    List<MenuTree> menuTrees = new ArrayList<>();
    for(Menu menu:menuList){
        MenuTree menuTree = new MenuTree(menu.getId(),menu.getPid(),menu.getName());
        menuTrees.add(menuTree);
    }
    //menuTree菜单转换有层级关系
    List<MenuTree> menuTreeList = new ArrayList<>();
    for(MenuTree menuTree:menuTrees){
        if(menuTree.getParentId()==1){
            menuTreeList.add(menuTree);
            continue;
        }
        for(MenuTree menuTree2:menuTreeList){
            if(menuTree.getParentId()==menuTree2.getId()){
             menuTree2.getChildren().add(menuTree);
             continue;
            }
        }
    }

List<MenuTree> menuTree = new ArrayList<>();
    for(MenuTree menuTree1:menuTrees){
        if(menuTree1.getParentId()==0){
            menuTree1.setChildren(menuTreeList);
            menuTree.add(menuTree1);
            break;
        }
    }
    return new AJAXResult(0,"操作成功",menuTree);
}
@RequestMapping("/menuRightShow")
    @ResponseBody
    public AJAXResult menuRightShow(MenuVo menuVo){
    if(menuVo.getPage()==null){
        menuVo.setPage(1);
    }
    if(menuVo.getLimit()==null){
        menuVo.setLimit(5);
    }
    PageInfo pageInfo=ms.findByWhere(menuVo);
    return new AJAXResult(0,"",pageInfo.getList(),pageInfo.getTotal());
}
@RequestMapping("/add")
    @ResponseBody
    public AJAXResult add(MenuVo menuVo){
    if(menuVo.getId()==null) {
        int i = ms.addMenu(menuVo);
        if (i == 0) {
            return AJAXResult.ADD_ERROR;
        }
        return AJAXResult.ADD_SUCCESS;
    }else{
        int j = ms.updateMenu(menuVo);
        if(j==0){
            return AJAXResult.UPDATE_ERROR;
        }
        return AJAXResult.UPDATE_SUCCESS;
    }
}
@RequestMapping("/checkChildren")
    @ResponseBody
    public AJAXResult checkChildren(MenuVo menuVo){
    int count = ms.findMenuByPid(menuVo.getId());
    return new AJAXResult(0,"",count);
}
@RequestMapping("/deleteById")
    @ResponseBody
    public AJAXResult deleteById(MenuVo menuVo){
    int row = ms.deleteById(menuVo.getId());
    if(row==0){
        return AJAXResult.DELETE_ERROR;
    }else{
        return AJAXResult.DELETE_SUCCESS;
    }
}
}
